/**
 * SparkWordCountJava.java
 *
 * @author: ZhuJiahui
 * @date: 2018/12/26 23:15
 */
package first;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.FlatMapFunction;
import org.apache.spark.api.java.function.Function2;
import org.apache.spark.api.java.function.PairFunction;
import scala.Tuple2;

import java.util.Arrays;
import java.util.Iterator;

/**
 * @description:
 * @author: ZhuJiahui
 * @version: 1.0
 */
public class SparkWordCountJava {
    public static void main(String[] args) {
        SparkConf conf = new SparkConf();
        conf.setMaster("local");
        conf.setAppName("WordCount");
        JavaSparkContext sc = new JavaSparkContext(conf);
        JavaRDD<String> fileRdd = sc.textFile(
                "D:/Local/IDEAWorkspace/spark/src/main/resources/data/word-count.txt");

        JavaRDD<String> wordRDD =
                fileRdd.flatMap(new FlatMapFunction<String, String>() {
                    @Override
                    public Iterator<String> call(String line) throws Exception {
                        return Arrays.asList(line.split(" ")).iterator();
                    }
                });

        JavaPairRDD<String, Integer> wordOneRDD =
                wordRDD.mapToPair(new PairFunction<String, String, Integer>() {
                    @Override
                    public Tuple2<String, Integer> call(String word)
                            throws Exception {
                        return new Tuple2<>(word, 1);
                    }
                });

        JavaPairRDD<String, Integer> wordCountRDD = wordOneRDD
                .reduceByKey(new Function2<Integer, Integer, Integer>() {
                    @Override
                    public Integer call(Integer i1, Integer i2)
                            throws Exception {
                        return i1 + i2;
                    }
                });

        JavaPairRDD<Integer, String> count2WordRDD = wordCountRDD.mapToPair(
                new PairFunction<Tuple2<String, Integer>, Integer, String>() {
                    @Override
                    public Tuple2<Integer, String> call(
                            Tuple2<String, Integer> tuple) throws Exception {
                        return new Tuple2<>(tuple._2, tuple._1);
                    }
                });

        JavaPairRDD<Integer, String> sortRDD = count2WordRDD.sortByKey(false);

        JavaPairRDD<String, Integer> resultRDD = sortRDD.mapToPair(
                new PairFunction<Tuple2<Integer, String>, String, Integer>() {
                    @Override
                    public Tuple2<String, Integer> call(
                            Tuple2<Integer, String> tuple) throws Exception {
                        return new Tuple2<>(tuple._2, tuple._1);
                    }
                });

        //
//        JavaRDD<String> fileRDD = sc.textFile("E:\\hello.txt");
//        JavaRDD<String> wordRdd = fileRDD.flatMap(line -> Arrays.asList(line.split(",")).iterator());
//        JavaPairRDD<String, Integer> wordOneRDD = wordRdd.mapToPair(word -> new Tuple2<>(word, 1));
//        JavaPairRDD<String, Integer> wordCountRDD = wordOneRDD.reduceByKey((x, y) -> x + y);
//        JavaPairRDD<Integer, String> count2WordRDD = wordCountRDD.mapToPair(tuple -> new Tuple2<>(tuple._2, tuple._1));
//        JavaPairRDD<Integer, String> sortRDD = count2WordRDD.sortByKey(false);
//        JavaPairRDD<String, Integer> resultRDD = sortRDD.mapToPair(tuple -> new Tuple2<>(tuple._2, tuple._1));
//        resultRDD.saveAsTextFile("E:\\result8");

        resultRDD.saveAsTextFile(
                "D:/Local/IDEAWorkspace/spark/src/main/resources/data/word-count-java-result");
    }

}
